package com.payment.utils;

import com.alibaba.fastjson.JSON;
import com.payment.entity.payment.paypalpay.response.ResponsePaypalPay;
import com.payment.entity.payment.paypalpay.response.ResponsePaypalPayData;
import com.payment.entity.payresults.travis.response.TravisCallbackResponse;
import com.payment.entity.payresults.travis.response.TravisCallbackResponseData;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

public class UtilsInterfacesPackage {
    private static Logger logger = Logger.getLogger(UtilsInterfacesPackage.class);

    //计算签名
    public String calculateSignature(String str) {

        //logger.info("签名体是：" + str);

        String sign = Md5Encrypt.md5(str);

        //logger.info("签名里面算出来的sign：" + sign);

        return sign;
    }

    /***
     *请求签名验证失败
     * @param connSeqNo
     * @param tradeTime
     * @param tradeType
     * @param
     * @param
     * @return
     */
    public String signatureFailure(String connSeqNo, String partnerCode, String tradeTime, String tradeType) {
        ResponsePaypalPayData data = new ResponsePaypalPayData();
        data.setConnSeqNo(connSeqNo);
        data.setPartnerCode(partnerCode);
        data.setTradeTime(tradeTime);
        data.setTradeType(tradeType);
        data.setTradeRstCode("0001");
        data.setTradeRstMessage("签名有误,请确认签名!");
        // 6，给ResponsePackage赋值
        ResponsePaypalPay resPackages = new ResponsePaypalPay();
        resPackages.setData(data);
        resPackages.setSign(MD5.calcMD5(JSON.toJSONString(data)));
        // 返回给调用者的报文
        String jsonString = JSON.toJSONString(resPackages);
        return jsonString;

    }

    /**
     * 请求报文不合法（少参数或者不允许为空的参数值为空）
     *
     * @param connSeqNo
     * @param curtime
     * @return
     */
    public String dataWeiKong(String connSeqNo, String curtime) {
        ResponsePaypalPayData data = new ResponsePaypalPayData();
        data.setConnSeqNo(connSeqNo);
        data.setTradeTime(curtime);
        data.setTradeRstCode("0002");
        data.setTradeRstMessage("请求报文不合法");
        // 6，给ResponsePackage赋值
        ResponsePaypalPay resPackages = new ResponsePaypalPay();
        resPackages.setData(data);
        resPackages.setSign(MD5.calcMD5(JSON.toJSONString(data)));
        // 返回给调用者的报文
        String jsonString = JSON.toJSONString(resPackages);
        return jsonString;
    }


    public String unknownError(String connSeqNo, String curtime, String tradeType) {
        ResponsePaypalPayData data = new ResponsePaypalPayData();
        data.setConnSeqNo(connSeqNo);
        data.setTradeType(tradeType);
        data.setTradeTime(curtime);
        data.setTradeRstCode("9999");
        data.setTradeRstMessage("接口出现未知错误，未成功返回！");

        ResponsePaypalPay userLogin = new ResponsePaypalPay();
        userLogin.setData(data);
        userLogin.setSign(Md5Encrypt.md5(JSON.toJSONString(data)));
        // 返回给调用者的报文
        String jsonString = JSON.toJSONString(userLogin);
        logger.info("响应报文：" + jsonString);
        return jsonString;
    }

    /***
     * 无需回传不成功状态的数据
     * @param connSeqNo
     * @param partnerCode
     * @param tradeTime
     * @param
     * @param tradeType
     * @return
     */
    public String unsuccessfulStatusData(String connSeqNo, String partnerCode, String tradeTime, String tradeType) {
        TravisCallbackResponseData data = new TravisCallbackResponseData();
        data.setConnSeqNo(connSeqNo);
        data.setPartnerCode(partnerCode);
        data.setTradeType(tradeType);
        data.setTradeTime(tradeTime);
        data.setTradeRstCode("0048");
        data.setTradeRstMessage("不成功的支付数据无需回传！");

        TravisCallbackResponse responseWeiXinPay = new TravisCallbackResponse();
        responseWeiXinPay.setData(data);
        responseWeiXinPay.setSign(MD5.calcMD5(JSON.toJSONString(data)));
        // 返回给调用者的报文
        String jsonString = JSON.toJSONString(responseWeiXinPay);
        logger.info("响应报文：" + jsonString);
        return jsonString;
    }

    public String repeatedReturn(String connSeqNo, String partnerCode, String tradeTime, String tradeType) {
        TravisCallbackResponseData data = new TravisCallbackResponseData();
        data.setConnSeqNo(connSeqNo);
        data.setPartnerCode(partnerCode);
        data.setTradeType(tradeType);
        data.setTradeTime(tradeTime);
        data.setTradeRstCode("0049");
        data.setTradeRstMessage("已经收到回传数据，无需重复回传！");

        TravisCallbackResponse responseWeiXinPay = new TravisCallbackResponse();
        responseWeiXinPay.setData(data);
        responseWeiXinPay.setSign(MD5.calcMD5(JSON.toJSONString(data)));
        // 返回给调用者的报文
        String jsonString = JSON.toJSONString(responseWeiXinPay);
        logger.info("响应报文：" + jsonString);
        return jsonString;
    }
}
